package code.lewdy.app.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import code.lewdy.util.RandomUtil;

/**
 * 会员推广链接
 * @author liuding,2012-4-27
 *
 * $LastChangedDate$
 * $Author$ 
 */
@Entity
@Table
public class PopularizeLink
{
    private long id;
    private long ownerId; //拥有者ID,一般为会员ID
    private String urlParamsString; //链接地址后缀参数字串,如regTop=abc%123 ,123为ownerId
    
    public static void main(String[] args)
    {
        System.out.println(getOwnerIdFromURL("regTop=LQXQNKLEEH058"));
    }
    /**生成推广链接
     * @param _ownerId
     * @return
     * @author liuding,2012-4-27
     */
    public static String produceParamsString(long _ownerId){
        String paramName = "regTop";
        return paramName+"="+RandomUtil.getRandomLetters(10)+"0"+_ownerId;
    }
    
    public static long getOwnerIdFromURL(String url){
        String paramName = "regTop";
        int index = url.indexOf(paramName);
        if(index < 0) 
            return 0l;
        String temp = url.substring(index+paramName.length()+1, url.length());
        temp = temp.substring(0, temp.contains("&") ? temp.indexOf("&") : temp.length());
        temp = temp.substring(temp.indexOf("0")+1, temp.length());
        return Long.parseLong(temp);
    }
    
    @Id
    @GeneratedValue
    public long getId()
    {
        return id;
    }
    public void setId(long id)
    {
        this.id = id;
    }
    @Column(length=250)
    public String getUrlParamsString()
    {
        return urlParamsString;
    }
    public void setUrlParamsString(String urlParamsString)
    {
        this.urlParamsString = urlParamsString;
    }
    public long getOwnerId()
    {
        return ownerId;
    }
    public void setOwnerId(long ownerId)
    {
        this.ownerId = ownerId;
    }
    
    
}
